﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class admin_ads_modi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //显示是否开放
            if (e.Row.Cells[6].Text == "True")
            {
                e.Row.Cells[6].Text = "<font color='red'>√</font>";
            }
            else
            {
                e.Row.Cells[6].Text = "×";
            }

            //显示是否租用
            if (e.Row.Cells[7].Text == "True")
            {
                e.Row.Cells[7].Text = "<font color='red'>√</font>";
            }
            else
            {
                e.Row.Cells[7].Text = "×";
            }

        }

    }
    protected void RadioButtonList_type_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList _radiobutton = (RadioButtonList)FormView1.FindControl("RadioButtonList_type");
        Panel _panel_address = (Panel)FormView1.FindControl("Panel_address");
        Panel _panel_url = (Panel)FormView1.FindControl("Panel_url");
        Panel _panel_code = (Panel)FormView1.FindControl("Panel_code");
        //Panel _panel_fileupload = (Panel)FormView1.FindControl("Panel_fileupload");
        FileUpload _fileupload = (FileUpload)FormView1.FindControl("MyFileUpload");
        Button _button = (Button)FormView1.FindControl("Button_fileUpload");
        if (_radiobutton.SelectedValue == "1")
        {
            _panel_address.Visible = true;
            _panel_url.Visible = true;
            _panel_code.Visible = false;
            _fileupload.Visible = true;
            _button.Visible = true;
            //_panel_fileupload.Visible = true;
        }
        else if (_radiobutton.SelectedValue == "2")
        {
            _panel_address.Visible = true;
            _panel_url.Visible = false;
            _panel_code.Visible = false;
            _fileupload.Visible = true;
            _button.Visible = true;
            //_panel_fileupload.Visible = true;
        }
        else if (_radiobutton.SelectedValue == "3")
        {
            _panel_address.Visible = true;
            _panel_url.Visible = true;
            _panel_code.Visible = false;
            _fileupload.Visible = false;
            _button.Visible = false;
            //_panel_fileupload.Visible = false;
        }
        else if (_radiobutton.SelectedValue == "4")
        {
            _panel_address.Visible = false;
            _panel_url.Visible = false;
            _panel_code.Visible = true;
        }
    }
    protected void Button_fileUpload_Click(object sender, EventArgs e)
    {
        FileUpload fileupload_pic = (FileUpload)FormView1.FindControl("MyFileUpload");
        Label label_Msg = (Label)FormView1.FindControl("Label_Msg");  //提示信息标签
        if (fileupload_pic.PostedFile.FileName.ToString() != "")
        {

            string photoname = fileupload_pic.PostedFile.FileName;
            int i = photoname.LastIndexOf(".");
            string file_next_name = photoname.Substring(i);
            if (file_next_name.ToLower() == ".exe" || file_next_name.ToLower() == ".com" || file_next_name.ToLower() == ".js" || file_next_name.ToLower() == ".bat" )
            {
                Response.Write("<script language='Javascript'>alert('上传文件出错,并不是合法文件！');</script>");
            }
            else
            {
                string filename = DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Second.ToString() + file_next_name; // 文件名称
                string filename_s = "s_" + filename; // 缩略图文件名称
                string webFilePath = Server.MapPath("../upload/ads/" + filename); // 服务器端文件路径
                //string webFilePath_s = Server.MapPath("/upload/ads/" + filename_s);　　// 服务器端 中 缩略图路径
                //string webFilePath_s_s = Server.MapPath("/upload/ads/s_" + filename_s);　　// 服务器端 小 缩略图路径


                if (!File.Exists(webFilePath))
                {
                    try
                    {
                        fileupload_pic.PostedFile.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
                        //Discuz.Web.global.global.MakeThumbnail(webFilePath, webFilePath_s, 280, 220, "Cut"); // 生成缩略图方法
                        //Discuz.Web.global.global.MakeThumbnail(webFilePath, webFilePath_s_s, 130, 95, "Cut"); // 生成缩略图方法

                        TextBox _TextBox_text1 = (TextBox)FormView1.FindControl("txtoraddressTextBox");
                        _TextBox_text1.Text = "/upload/ads/" + filename.Trim();

                        //Image ImageUpload = (Image)FormView1.FindControl("Image1");
                        //ImageUpload.Visible = true;
                        //ImageUpload.ImageUrl = "/upload/ads/" + filename;


                        label_Msg.Text = "提示：上传成功,大小：" + fileupload_pic.PostedFile.ContentLength / 1024 + "K";//，文件类型为：" + fileupload_pic.PostedFile.ContentType + "
                    }
                    catch (Exception ex)
                    {
                        label_Msg.Text = "文件上传失败,失败原因：" + ex.Message;
                    }
                }
                else
                {
                    label_Msg.Text = "文件已经存在,请重新上传!";
                }
            }
        }
        else
        {
            label_Msg.Text = "上传文件不能为空!";
            //Response.Write("<script language='Javascript'>alert('上传文件不能为空！');</script>");
        }
    }
    protected void FormView1_DataBound(object sender, EventArgs e)
    {
        RadioButtonList _radiobutton = (RadioButtonList)FormView1.FindControl("RadioButtonList_type");
        Panel _panel_address = (Panel)FormView1.FindControl("Panel_address");
        Panel _panel_url = (Panel)FormView1.FindControl("Panel_url");
        Panel _panel_code = (Panel)FormView1.FindControl("Panel_code");
        //Panel _panel_fileupload = (Panel)FormView1.FindControl("Panel_fileupload");
        FileUpload _fileupload = (FileUpload)FormView1.FindControl("MyFileUpload");
        Button _button = (Button)FormView1.FindControl("Button_fileUpload");
        if (_radiobutton.SelectedValue == "1")
        {
            _panel_address.Visible = true;
            _panel_url.Visible = true;
            _panel_code.Visible = false;
            _fileupload.Visible = true;
            _button.Visible = true;
            //_panel_fileupload.Visible = true;
        }
        else if (_radiobutton.SelectedValue == "2")
        {
            _panel_address.Visible = true;
            _panel_url.Visible = false;
            _panel_code.Visible = false;
            _fileupload.Visible = true;
            _button.Visible = true;
            //_panel_fileupload.Visible = true;
        }
        else if (_radiobutton.SelectedValue == "3")
        {
            _panel_address.Visible = true;
            _panel_url.Visible = true;
            _panel_code.Visible = false;
            _fileupload.Visible = false;
            _button.Visible = false;
            //_panel_fileupload.Visible = false;
        }
        else if (_radiobutton.SelectedValue == "4")
        {
            _panel_address.Visible = false;
            _panel_url.Visible = false;
            _panel_code.Visible = true;
        }
    }
    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        RadioButtonList _radiobutton = (RadioButtonList)FormView1.FindControl("RadioButtonList_type");
        TextBox _textbox_url=(TextBox)FormView1.FindControl("urlTextBox");
        TextBox _textbox_address = (TextBox)FormView1.FindControl("txtoraddressTextBox");
        Label _label_width=(Label)FormView1.FindControl("Label_width");
        Label _label_height=(Label)FormView1.FindControl("Label_height");


        if (_radiobutton.SelectedValue == "1")
        {
            e.NewValues["content"]="<a href=" + _textbox_url.Text.Trim() + " target=_blank><img src='" + _textbox_address.Text.Trim() + "'  border=0 width="+_label_width.Text.Trim() +" height="+_label_height.Text.Trim() +"></a>";
        }
        else if (_radiobutton.SelectedValue == "2")
        {
            e.NewValues["content"] = "<object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 width='" + _label_width.Text.Trim() + "' height='" + _label_height.Text.Trim() + "'><param name=movie value='" + _textbox_address.Text.Trim() + "'><param name=quality value=high><embed src='" + _textbox_address.Text.Trim() + "' quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width='" + _label_width.Text.Trim() + "' height='" + _label_height.Text.Trim() + "'></embed></object>";
        }
        else if (_radiobutton.SelectedValue == "3")
        {
            e.NewValues["content"]="<a href="+ _textbox_url.Text.Trim() + " target=_blank>"+ _textbox_address.Text.Trim() +"</a>";
        }
        //else if (_radiobutton.SelectedValue == "4")
        //{
        //    e.NewValues["content"]="";
        //}
    }
    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        string url = "ads_manage.aspx";
        Response.Redirect(url);

    }
}
